package cn.iocoder.yudao.module.promotion.controller.app.paymentterm;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.promotion.controller.admin.paymentterm.vo.PaymentTermPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.paymentterm.vo.PaymentTermRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.paymentterm.vo.PaymentTermSaveReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.paymentterm.vo.AppPaymentTermPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.paymentterm.vo.AppPaymentTermRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.paymentterm.vo.AppPaymentTermSaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.paymentterm.PaymentTermDO;
import cn.iocoder.yudao.module.promotion.service.paymentterm.PaymentTermService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 收款方式")
@RestController
@RequestMapping("/promotion/payment-term")
@Validated
public class AppPaymentTermController {

    @Resource
    private PaymentTermService paymentTermService;

    @PutMapping("/update")
    @Operation(summary = "更新收款方式")
    public CommonResult<Boolean> updatePaymentTerm(@RequestBody PaymentTermSaveReqVO updateReqVO) {
        paymentTermService.updatePaymentTerm(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收款方式")
    @Parameter(name = "memberId", description = "编号", required = true, example = "1024")
    public CommonResult<AppPaymentTermRespVO> getPaymentTerm(@RequestParam("memberId") Long memberId) {
        PaymentTermDO paymentTerm = paymentTermService.getPaymentTermByMemberId(memberId);
        if (null == paymentTerm) {
            Long id = paymentTermService.createPaymentTerm(new PaymentTermSaveReqVO().setMemberId(memberId));
            paymentTerm = paymentTermService.getPaymentTerm(id);
        }
        return success(BeanUtils.toBean(paymentTerm, AppPaymentTermRespVO.class));
    }

    @GetMapping("/getCompanyPaymentTerm")
    @Operation(summary = "获得收款方式分页")
    public CommonResult<List<PaymentTermRespVO>> getCompanyPaymentTerm(@Valid PaymentTermPageReqVO pageReqVO) {
        List<PaymentTermDO> result = paymentTermService.getCompanyPaymentTerm(pageReqVO);
        return success(BeanUtils.toBean(result, PaymentTermRespVO.class));
    }

    @GetMapping("/getLockUserPaymentTerm")
    @Operation(summary = "获得收款方式分页")
    public CommonResult<List<PaymentTermRespVO>> getLockUserPaymentTerm(@Valid PaymentTermPageReqVO pageReqVO) {
        List<PaymentTermDO> result = paymentTermService.getLockUserPaymentTerm(pageReqVO);
        return success(BeanUtils.toBean(result, PaymentTermRespVO.class));
    }

}